<?php

namespace App\Validators\System;

use App\Validators\PassesValidator;
use Illuminate\Support\Facades\Input;
use \Prettus\Validator\Contracts\ValidatorInterface;
use \Prettus\Validator\LaravelValidator;

class UserValidator extends LaravelValidator
{
    use PassesValidator;

    protected $rulesMessages=[
        'id.required' => 'ID是必传的',
        'id.exists' => '用户不存在',
        'name.required' => '真实姓名是必填的',
        'phone.unique' => '该手机已经存在',
        'phone.phone' => '手机号格式错误',
        'phone.required' => '手机号是必填的',
        'password.required' => '密码是必填的',
        'password.min' => '密码格式错误',
        'password.confirmed' => '验证密码与密码不一致',
        'major_department_id.required' => '部门是必选的',
        'major_station_id.required' => '岗位是必选的',
        'email.unique' => '该邮箱已经存在',
        ];

    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'name' => 'required|max:255',
            'phone' => 'required|phone|unique:users',
            'email' => 'unique:users',
            'password' => 'required|min:6',
            'major_department_id' => 'required',
            'major_station_id' => 'required',
        ],
        ValidatorInterface::RULE_UPDATE => [
            'name' => 'required|max:255',
            'phone' => 'required|phone|unique:users,phone,{id}',
            'email' => 'unique:users,email,{id}',
            'password' => 'min:6',
            'major_department_id' => 'required',
            'major_station_id' => 'required',
        ],
        "UPDATE_PASSWORD_WITH_ID" => [
            'old_password' => 'required|max:255',
            'password' => 'required|min:6|confirmed',
            'id' => 'required|exists:users,id',
        ],
   ];

    public function callBackGetRules(array $rules){
        $result=[];
        $value_change = array('{id}'=>Input::get('id'));
        foreach ($rules as $key=>$value){
            if(is_string($value) && str_contains($value,'{id}')){
                $result[$key]=strtr($value,$value_change);
            }else{
                $result[$key]=$value;
            }
        }
        return $result;
    }
}
